home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 1
/
Gekikoh Dennoh Club Vol. 1 (Japan).7z
/
Gekikoh Dennoh Club Vol. 1 (Japan) (Track 1).bin
/
kowin
/
archive
/
apl
/
pedp.lzh
/
pattern.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-01-13
|
15KB
|
661 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys_doslib.h>
#include "wlib.h"
#include "parts.h"
#include "winop.h"
#define MESSAGE_TABLE_X 32
#define MESSAGE_TABLE_Y 128
#define EDIT_RATIO 8
#define DEFAULT_EDIT 256
#define TYPE_SOURCE 1
#define TYPE_INCLUDE 2
#define max( a, b ) ( (a) > (b) ? (a) : (b) )
static Sheet Pattern ;
static Sheet EditPattern ;
static ClipClass PatternClip ;
static ClipClass EditPatternClip ;
static char FileName[100] ;
static int Xsize, Ysize ;
static int CurrentCode ;
static int MessageTableX ;
static int ScrollFlag ;
static int OutPutType ;
static WindowID InputWP ;
extern void SendUserEvent( WindowID, EventInfo*, Sheet* );
/*
proto -s pattern.c > temp
*/
static int EventExec( WindowID, EventInfo* );
static void EditorClose( WindowID );
static void EditorResize( WindowID, int, int, int, int );
static int EditExec( WindowID, EventInfo* );
static int MessageExec( WindowID, EventInfo* );
static void SaveOK( void );
static int OKexec( WindowID, EventInfo* );
static int GetSize( void );
static int PatternRead( Sheet* );
static void PatternToEdit( void );
static void PatternWrite( void );
int RightCode= 1;
/* エディットウインドウのオープン */
int PatternOpen( inputwp, name, x, y, oflag, sp )
WindowID inputwp ;
char *name ;
int x, y ;
int oflag ;
Sheet *sp ;
{
int h, v, sw ;
char *p ;
WindowID wp, cwp ;
CurrentCode = 0 ;
InputWP = inputwp ;
OutPutType = oflag ;
strcpy( FileName, name );
strlwr( FileName );
p = strrchr( FileName, '\\' );
if ( p == NULL )
FileName[0] = toupper( FileName[0] );
else
p[1] = toupper( p[1] );
if ( strchr( FileName, '.' ) == NULL )
{
if ( OutPutType == TYPE_SOURCE )
strcat( FileName, ".C" );
else
strcat( FileName, ".INC" );
}
if ( sp == NULL )
{
if ( GetSize() == FALSE )
{
Xsize = x ;
Ysize = y ;
}
if ( Xsize <= 0 && Ysize <= 0 )
{
ErrorMessage( "サイズが不正です。", ERROR_OK );
return( FALSE );
}
}
else
{
Xsize = sp->h ;
Ysize = sp->v ;
}
if ( PatternRead( sp ) == FALSE )
return( FALSE );
MessageTableX = max( Xsize+16, MESSAGE_TABLE_X );
if ( Xsize < 32 && Ysize < 32 )
{
h = Xsize * EDIT_RATIO ;
v = max( Ysize * EDIT_RATIO, MESSAGE_TABLE_Y );
sw = Close|Push ;
ScrollFlag = FALSE ;
}
else
{
h = DEFAULT_EDIT ;
v = DEFAULT_EDIT ;
sw = Resize|Close|Push|Zoom ;
ScrollFlag = TRUE ;
}
WindowGetPosition( InputWP, &x, &y );
wp = WindowTitleOpen( x, y, h + MessageTableX, v, NULL, FileName, sw, EventExec );
if ( ScrollFlag )
{
cwp = WindowScrollOpen( 0, 0, h, v, wp, Xsize*EDIT_RATIO, Ysize*EDIT_RATIO,
DirectionX|DirectionY, 16, 16, EditExec );
}
else
cwp = WindowOpen( 0, 0, h, v, wp, EditExec );
cwp = WindowOpen( h, 0, MessageTableX, v, wp, MessageExec );
WindowRedraw( wp );
return( TRUE );
}
/* エディットウインドウのイベント処理 */
static int EventExec( wp, info )
WindowID wp ;
EventInfo *info ;
{
DrawBuf buf[1] ;
WindowID cwp ;
int x, y, size ;
int hh, vv ;
Sheet *sp ;
static ZoomBuffer zoom ;
switch( info->option )
{
case EventOpen :
ZoomInit( wp, &zoom );
WindowSetEventAttr( wp, EventAttrDefault|EventUserON );
return( TRUE );
case EventRedraw :
DrawSetClear( buf, 1 );
WindowDraw( wp, buf, 1 );
cwp = WindowGetChildTop( wp );
WindowRedraw( cwp );
cwp = WindowGetNext( cwp );
WindowRedraw( cwp );
return( TRUE );
case EventClose :
SaveOK();
EditorClose( wp );
return( TRUE );
case EventZoom :
ZoomOperation( wp, info );
return( TRUE );
case EventResize :
EditorResize( wp, info->x, info->y, info->h, info->v );
return( TRUE );
case EventMouseSwitch :
if ( info->LeftStat )
{
WindowGetViewSize( wp, &hh, &vv );
if ( ScrollFlag && info->x > hh-10 && info->y > vv - 10 )
{
WindowResizeOperation( wp, info );
return( TRUE );
}
}
cwp = WindowGetChild( wp, info );
WindowSendEvent( cwp, info );
return( TRUE );
case EventKey:
switch( info->KeyCode ){
case FuncKeyF(10):
PatternWrite();
EditorClose( wp );
break;
default:
cwp = WindowGetChild( wp, info );
WindowSendEvent( cwp, info );
}
return TRUE;
case EventUser :
if ( info->ComData == UserSheet )
{
sp = info->ComBuffer ;
if ( sp->h == Xsize && sp->v == Ysize )
{
size = Pattern.hword * Pattern.v * sizeof( short ) ;
memcpy( Pattern.buf1, sp->buf1, size );
memcpy( Pattern.buf2, sp->buf2, size );
PatternToEdit();
WindowRedraw( wp );
}
return( TRUE );
}
return( FALSE );
}
return( FALSE );
}
/* エディットウインドウの終了処理 */
static void EditorClose( wp )
WindowID wp ;
{
int x, y ;
WindowGetPosition( wp, &x, &y );
WindowClose( wp );
MFREE( Pattern.buf1 );
MFREE( Pattern.buf2 );
MFREE( EditPattern.buf1 );
MFREE( EditPattern.buf2 );
WindowMove( InputWP, x, y );
WindowSetAttr( InputWP, 0 );
}
/* エディットウインドウのリサイズ処理 */
static void EditorResize( wp, x, y, h, v )
WindowID wp ;
int x, y, h, v ;
{
int ch, cv, vh, vv, maxh, maxv ;
WindowID cwp ;
cwp = WindowGetChildBottom( wp );
WindowGetViewSize( cwp, &vh, &vv );
WindowGetSize( cwp, &ch, &cv );
maxh = Xsize * EDIT_RATIO + ch - vh + MessageTableX ;
maxv = Ysize * EDIT_RATIO + cv - vv ;
if ( h > maxh )
h = maxh ;
if ( v > maxv )
v = maxv ;
cwp = WindowGetChildTop( wp );
WindowResize( cwp, h - MessageTableX, 0, MessageTableX, v );
cwp = WindowGetNext( cwp );
WindowResize( cwp, 0, 0, h - MessageTableX, v );
WindowResize( wp, x, y, h, v );
}
/* エディットウインドウのイベント処理 */
static int EditExec( wp, info )
WindowID wp ;
EventInfo *info ;
{
WindowID cwp ;
int i, code, x, y, x2, y2 ;
DrawBuf buf[2] ;
switch( info->option )
{
case EventRedraw :
DrawSetClear( buf, 1 );
DrawSetPut( buf+1, 0, 0, &EditPattern );
WindowDraw( wp, buf, 2 );
return( TRUE );
case EventKey:
switch( info->KeyCode ){
case 0x1b:
case '0': code= 0; goto _Dot;
case '1': code= 1; goto _Dot;
case '2': code= 2; goto _Dot;
case '3': code= 3; goto _Dot;
case ' ': code= CurrentCode; goto _Dot;
}
return TRUE;
case EventMouseSwitch :
if ( info->LeftStat )
code = CurrentCode ;
else if ( info->RightStat )
code = RightCode ;
else
return( TRUE );
_Dot:
x = info->x / EDIT_RATIO ;
y = info->y / EDIT_RATIO ;
if ( 0 <= x && x < Xsize && 0 <= y && y < Ysize )
{
SheetLine( &Pattern, &PatternClip, x, y, x, y, code, OptionFill );
DrawSetLine( buf, 8+x, 110+y, 8+x, 110+y, code, OptionFill );
cwp = WindowGetLast( wp );
WindowDraw( cwp, buf, 1 );
x *= EDIT_RATIO ;
y *= EDIT_RATIO ;
x2 = x + EDIT_RATIO - 1 ;
y2 = y + EDIT_RATIO - 1 ;
DrawSetLine( buf, x, y, x2, y2, code, OptionFill );
SheetLine( &EditPattern, &EditPatternClip, x, y, x2, y2,
code, OptionFill );
WindowDraw( wp, buf, 1 );
}
return( TRUE );
}
return( FALSE );
}
/* メッセージウインドウのイベント処理 */
static int MessageExec( wp, info )
WindowID wp ;
EventInfo *info ;
{
int i, x, y ;
DrawBuf dbuf[2] ;
static DrawBuf *bufp, buf[28] ;
static ClipClass clip[3] ;
static char *msg[3] = { "SAVE", "QUIT", "MOVE" } ;
switch( info->option )
{
case EventOpen :
bufp = buf ;
DrawSetClear( bufp++, 1 );
for( i = 0 ; i < 4 ; ++i )
{
DrawSetLine( bufp++, 4, i*12+4, 4+24, i*12+4+11, i, OptionFill );
if ( i == CurrentCode )
DrawSetLine( bufp++, 4, i*12+4, 4+11, i*12+4+11, ShadowDown, OptionShadow );
else
DrawSetLine( bufp++, 4, i*12+4, 4+11, i*12+4+11, ShadowUp, OptionShadow );
if ( i == RightCode )
DrawSetLine( bufp++, 4+12, i*12+4, 4+24, i*12+4+11, ShadowDown, OptionShadow );
else
DrawSetLine( bufp++, 4+12, i*12+4, 4+24, i*12+4+11, ShadowUp, OptionShadow );
}
for( i = 0 ; i < 3 ; ++i )
{
DrawSetSymbol( bufp++, 4, 60+14*i, msg[i], AttrDefault, 12 );
DrawSetLine( bufp++, 3, 59+14*i, 4+24, 60+14*i+12, ShadowUp, OptionShadow );
ClipSet( &clip[i], 4, 60+14*i, 24, 12 );
}
DrawSetLine( bufp++, 7, 109, 8+Xsize, 110+Ysize, ShadowDown, OptionShadow );
DrawSetPut( bufp++, 8, 110, &Pattern );
return( TRUE );
case EventRedraw :
WindowDraw( wp, buf, bufp-buf );
return( TRUE );
case EventMouseSwitch :
if ( ClipInner( &clip[0], info->x, info->y ) )
{
PatternWrite();
EditorClose( WindowGetParent( wp ) );
}
else if ( ClipInner( &clip[1], info->x, info->y ) )
{
EditorClose( WindowGetParent( wp ) );
}
else if ( ClipInner( &clip[2], info->x, info->y ) )
{
SendUserEvent( WindowGetParent( wp ), info, &Pattern );
}
else
{
y = ( info->y - 4 ) / 12 ;
if ( 0 <= y && y < 4 )
{
if( info->LeftStat ){
DrawSetLine( dbuf, 4, CurrentCode*12+4, 4+11, CurrentCode*12+4+11,
ShadowUp, OptionShadow );
CurrentCode = y ;
DrawSetLine( dbuf+1, 4, CurrentCode*12+4, 4+11, CurrentCode*12+4+11,
ShadowDown, OptionShadow );
WindowDraw( wp, dbuf, 2 );
}else if( info->RightStat ){
DrawSetLine( dbuf, 4+12, RightCode*12+4, 4+24, RightCode*12+4+11,
ShadowUp, OptionShadow );
RightCode = y ;
DrawSetLine( dbuf+1, 4+12, RightCode*12+4, 4+24, RightCode*12+4+11,
ShadowDown, OptionShadow );
WindowDraw( wp, dbuf, 2 );
}
}
}
return( TRUE );
}
return( FALSE );
}
static void SaveOK()
{
EventInfo Info ;
WindowID wp ;
ClipClass yes, no ;
int hx, hy ;
WindowGetRootScroll( &hx, &hy );
wp = WindowSimpleOpen( hx+234, hy+200, 300, 80, NULL, OKexec );
WindowRedraw( wp );
ClipSet( &yes, 60-30, 50-4, 60, 24 );
ClipSet( &no, 240-30, 50-4, 60, 24 );
for(;;)
{
WindowGetEventInfo( &Info );
if ( Info.LeftStat && WindowGetChild( WindowRootID, &Info ) == wp )
{
if ( ClipInner( &yes, Info.x, Info.y ) )
{
WindowClose( wp );
PatternWrite();
return ;
}
if ( ClipInner( &no, Info.x, Info.y ) )
{
WindowClose( wp );
return ;
}
}else if( Info.option == EventKey ){
switch( Info.KeyCode ){
case 'y':
case 'Y':
WindowClose( wp );
PatternWrite();
return;
case 'n':
case 'N':
WindowClose( wp );
return;
}
}
}
}
static int OKexec( wp, info )
WindowID wp ;
EventInfo *info ;
{
DrawBuf buf[10] ;
switch( info->option )
{
case EventRedraw :
DrawSetClear( buf, 1 );
DrawSetSymbol( buf+1, 30, 10, "パターンデータを保存しますか?", AttrDefault, 16 );
DrawSetSymbol( buf+2, 60-24, 50, "YES", AttrDefault, 16 );
DrawSetSymbol( buf+3, 240-16, 50, "NO", AttrDefault, 16 );
DrawSetLine( buf+4, 60-30, 50-4, 60+30, 50+20, ShadowUp, OptionShadow );
DrawSetLine( buf+5, 240-30, 50-4, 240+30, 50+20, ShadowUp, OptionShadow );
WindowDraw( wp, buf, 6 );
return( TRUE );
}
return( FALSE );
}
static int GetSize()
{
FILE *fp ;
char buf[50] ;
fp = fopen( FileName, "r" );
if ( fp == NULL )
return( FALSE );
do
{
if ( fscanf( fp, "%s", buf ) == 0 )
return( FALSE );
}
while( strcmp( buf, "Sheet" ) != 0 );
fscanf( fp, "%s", buf ); /* Pattern Name */
fscanf( fp, "= {%d,", &Xsize );
fscanf( fp, "%d", &Ysize );
fclose( fp );
if ( Xsize > 0 && Ysize > 0 )
return( TRUE );
else
{
ErrorMessage( "ファイルの構造が不正です。", ERROR_OK );
return( FALSE );
}
}
/* パターンの読み込み */
static int PatternRead( sp )
Sheet *sp ;
{
int i, n, editsize, size ;
short *p ;
char buf[100] ;
FILE *fp ;
Pattern.h = Xsize ;
Pattern.v = Ysize ;
Pattern.hword = ( Xsize - 1 ) / 16 + 1 ;
size = Pattern.hword * Pattern.v * sizeof( short ) ;
Pattern.buf1 = MALLOC( size );
Pattern.buf2 = MALLOC( size );
ClipSet( &PatternClip, 0, 0, Xsize, Ysize );
EditPattern.h = Xsize * 8 ;
EditPattern.v = Ysize * 8 ;
EditPattern.hword = ( Xsize * 8 - 1 ) / 16 + 1 ;
editsize = EditPattern.hword * EditPattern.v * sizeof( short ) ;
EditPattern.buf1 = MALLOC( editsize );
EditPattern.buf2 = MALLOC( editsize );
ClipSet( &EditPatternClip, 0, 0, Xsize*8, Ysize*8 );
if ( EditPattern.buf2 > 0x80000000 )
{
ErrorMessage( "メモリが足りません。", ERROR_OK );
return( FALSE );
}
if ( sp != NULL )
{
memcpy( Pattern.buf1, sp->buf1, size );
memcpy( Pattern.buf2, sp->buf2, size );
PatternToEdit();
return( TRUE );
}
fp = fopen( FileName, "r" );
if ( fp == NULL )
{
memset( Pattern.buf1, 0xFF, size );
memset( Pattern.buf2, 0x00, size );
memset( EditPattern.buf1, 0xFF, editsize );
memset( EditPattern.buf2, 0x00, editsize );
return( TRUE );
}
size /= sizeof( short );
do
{
fgets( buf, 80, fp );
}
while( strncmp( buf, "static", 6 ) != 0 );
p = Pattern.buf1 ;
for( i = 0 ; i < size ; ++i )
{
fscanf( fp, "0x%X, ", &n );
*p++ = n ;
}
do
{
fgets( buf, 80, fp );
}
while( strncmp( buf, "static", 6 ) != 0 );
p = Pattern.buf2 ;
for( i = 0 ; i < size ; ++i )
{
fscanf( fp, "0x%X, ", &n );
*p++ = n ;
}
fclose( fp );
PatternToEdit();
return( TRUE );
}
static void PatternToEdit()
{
int x, y, code, bit ;
short *p1, *p2 ;
p1 = Pattern.buf1 ;
p2 = Pattern.buf2 ;
for( y = 0 ; y < Ysize ; ++y )
{
for( x = 0 ; x < Xsize ; ++x )
{
bit = x % 16 ;
code = 0 ;
if ( *p1 & ( 0x8000 >> bit ) )
code += 1 ;
if ( *p2 & ( 0x8000 >> bit ) )
code += 2 ;
SheetLine( &EditPattern, &EditPatternClip, x*8, y*8, x*8+7, y*8+7,
code, OptionFill );
if ( bit == 15 )
{
p1++ ;
p2++ ;
}
}
if ( x % 16 )
{
p1++ ;
p2++ ;
}
}
}
static void PatternWrite()
{
int i, size ;
FILE *fp ;
char *cp, *name ;
unsigned short *p ;
size = Pattern.hword * Pattern.v ;
fp = fopen( FileName, "w" );
if ( fp == NULL )
{
ErrorMessage( "ファイルが出力できません。", ERROR_OK );
return ;
}
cp = strchr( FileName, '.' );
if ( cp != NULL )
*cp = '\0' ;
name = strrchr( FileName, '\\' );
if ( name == NULL )
name = FileName ;
else
name ++ ;
if ( OutPutType == TYPE_SOURCE )
fprintf( fp, "#include \"wlib.h\"\n\n" );
else
{
fprintf( fp, "#ifndef CLASS\n" );
fprintf( fp, "#define CLASS static\n" );
fprintf( fp, "#endif\n" );
}
p = Pattern.buf1 ;
fprintf( fp, "static\tshort\t%s1[] = {\n", name );
fprintf( fp, "\t0x%04X", *p++ );
for( i = 1 ; i < size ; ++i )
fprintf( fp, ", 0x%04X", *p++ );
fprintf( fp, "\n};\n" );
p = Pattern.buf2 ;
fprintf( fp, "static\tshort\t%s2[] = {\n", name );
fprintf( fp, "\t0x%04X", *p++ );
for( i = 1 ; i < size ; ++i )
fprintf( fp, ", 0x%04X", *p++ );
fprintf( fp, "\n};\n" );
if ( OutPutType == TYPE_INCLUDE )
fprintf( fp, "CLASS\t" );
fprintf( fp, "Sheet\t%sPattern = { %d, %d, %d, %s1, %s2 };\n",
name, Pattern.h, Pattern.v, Pattern.hword, name, name );
fclose( fp );
}